package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yunlang.zhangZhongFei.entity.User;
import cn.yunlang.zhangZhongFei.entity.UserLike;
import cn.yunlang.zhangZhongFei.mapper.UserLikeMapper;
import cn.yunlang.zhangZhongFei.service.UserLikeService;
import cn.yunlang.zhangZhongFei.service.UserService;
import cn.yunlang.zhangZhongFei.vo.request.UserLikePageVo;
import cn.yunlang.zhangZhongFei.vo.request.UserLikeVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 用户关注表 服务实现类
 *
 * @author ·
 * @since 2025-05-26 14:50:46
 */
@Service
public class UserLikeServiceImpl extends ServiceImpl<UserLikeMapper, UserLike>
    implements UserLikeService {

  @Resource private UserService userService;

  public R<IPage<UserLikePageVo>> pageLike(Integer pageNo, Integer pageSize, Long userId) {
    if (pageNo == null || pageSize == null || userId == null) {
      return R.error(400, "分页参数和用户ID不能为空");
    }
    Page<UserLike> page =
        this.page(
            new Page<>(pageNo, pageSize),
            new LambdaQueryWrapper<UserLike>()
                .eq(UserLike::getUserId, userId)
                .orderByDesc(UserLike::getCreateTime));
    Page<UserLikePageVo> pageRes = new Page<>();
    BeanUtils.copyProperties(page, pageRes);
    List<UserLikePageVo> resList = new ArrayList<>();
    List<Long> userList =
        page.getRecords().stream().map(UserLike::getLikedUserId).collect(Collectors.toList());
    Map<Long, Long> map =
        page.getRecords().stream()
            .collect(Collectors.toMap(UserLike::getLikedUserId, UserLike::getId));
    List<User> users = userService.listByIds(userList);
    Map<Long, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
    page.getRecords()
        .forEach(
            userLike -> {
              UserLikePageVo item = new UserLikePageVo();
              User user = userMap.get(userLike.getLikedUserId());
              if (user != null) {
                BeanUtils.copyProperties(user, item);
              }
              item.setId(map.get(userLike.getLikedUserId()));
              item.setUserId(userId);
              resList.add(item);
            });
    pageRes.setRecords(resList);
    return R.success(pageRes);
  }

  public R<IPage<UserLike>> likedPage(Integer pageNo, Integer pageSize, Long userId) {
    if (pageNo == null || pageSize == null || userId == null) {
      return R.error(400, "分页参数和用户ID不能为空");
    }
    IPage<UserLike> page =
        this.page(
            new Page<>(pageNo, pageSize),
            new LambdaQueryWrapper<UserLike>()
                .eq(UserLike::getLikedUserId, userId)
                .orderByDesc(UserLike::getCreateTime));
    return R.success(page);
  }

  public R<UserLike> getOne(Long userLikeId) {
    if (userLikeId == null) {
      return R.error(400, "ID不能为空");
    }
    UserLike userLike = this.getById(userLikeId);
    if (userLike == null) {
      return R.error(404, "未找到关注记录");
    }
    return R.success(userLike);
  }

  public R<String> doLike(UserLikeVo userLikeVo) {
    if (userLikeVo == null
        || userLikeVo.getUserId() == null
        || userLikeVo.getLikedUserId() == null) {
      return R.error(400, "用户ID和被关注用户ID不能为空");
    }
    UserLike one =
        this.getOne(
            new LambdaQueryWrapper<UserLike>()
                .eq(UserLike::getLikedUserId, userLikeVo.getLikedUserId())
                .eq(UserLike::getUserId, userLikeVo.getUserId()));
    boolean back;
    if (one == null) {
      back = this.like(userLikeVo);
    } else {
      back =
          this.remove(
              new LambdaQueryWrapper<UserLike>()
                  .eq(UserLike::getLikedUserId, userLikeVo.getLikedUserId())
                  .eq(UserLike::getUserId, userLikeVo.getUserId()));
    }
    return back ? R.success("取消成功") : R.error(500, "取消失败");
  }

  public R<String> likeUser(UserLikeVo userLikeVo) {
    if (userLikeVo == null
        || userLikeVo.getUserId() == null
        || userLikeVo.getLikedUserId() == null) {
      return R.error(400, "用户ID和被关注用户ID不能为空");
    }
    boolean save = this.like(userLikeVo);
    return save ? R.success("关注成功") : R.error(500, "关注失败");
  }

  public R<String> removeLikeUser(UserLikeVo userLikeVo) {
    if (userLikeVo == null
        || userLikeVo.getUserId() == null
        || userLikeVo.getLikedUserId() == null) {
      return R.error(400, "用户ID和被关注用户ID不能为空");
    }
    boolean remove =
        this.remove(
            new LambdaQueryWrapper<UserLike>()
                .eq(UserLike::getLikedUserId, userLikeVo.getLikedUserId())
                .eq(UserLike::getUserId, userLikeVo.getUserId()));
    return remove ? R.success("取消成功") : R.error(500, "取消失败");
  }

  @Override
  public boolean like(UserLikeVo userLikeVo) {
    UserLike userLike = new UserLike();
    BeanUtils.copyProperties(userLikeVo, userLike);
    return save(userLike);
  }
}
